package cn.gjxblog.mq.mechanism.dlx;

import cn.gjxblog.mq.utils.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/7/4 11:48
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //1 创建连接
        Connection connection = ConnectionUtils.getConnection();

        //2 通过connection创建channel
        Channel channel = connection.createChannel();

        //3 通过channel发送数据
        String exchangeName = "test_dlx_exchange";
        String routingKey = "dlx.test";
        String msg = "Hello RabbitMQ!";

        AMQP.BasicProperties build = new AMQP.BasicProperties().builder()
                .deliveryMode(2)
                .contentEncoding("UTF-8")
                //过期时间
                .expiration("10000")
                .build();

        channel.basicPublish(exchangeName,routingKey,build,msg.getBytes());

        //4 关闭相关连接
        channel.close();
        connection.close();
    }
}
